﻿using HtmlAgilityPack;//HtmlAgilityPack
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using AspNetCore.MyCrawler.Utility;
using MyCrawler.DataService;
using AspNetCore.MyCrawler.Model;
using System.Threading;
using System.Collections;

namespace AspNetCore.MyCrawler
{
    /// <summary>
    /// 
    /// </summary>
    public class CourseSearch : ISearch
    {
        private Logger logger = new Logger(typeof(CourseSearch));
        private WarnRepository warnRepository = new WarnRepository();
        private CourseRepository courseRepository = new CourseRepository();
        private TencentCategoryEntity category = null;

        public CourseSearch(TencentCategoryEntity _category)
        {
            category = _category;
        }

        public void Crawler()
        {
            try
            {
                if (string.IsNullOrEmpty(category.Url))
                {
                    warnRepository.SaveWarn(category, string.Format("Url为空,Name={0} Level={1} Url={2}", category.Name, category.CategoryLevel, category.Url));
                    return;
                }
                {

                    //模拟网页请求 获取网页数据 对网页数据进行装载 结构解析 然后写算法 提取相关URL 装载所有的网页数据并提取关键信息 

                    ///非常耗时；
                    ///如何来提高性能？


                    #region 分页的数据
                    //1.先确定一共有多少页
                    //2.逐页获取数据然后保存入库 
                    string pageUrl = $"{Constant.TencentClassUrl}{category.Url}"; 
                    List<CourseEntity> courselist = new List<CourseEntity>(); 
                    string html = HttpHelper.DownloadUrl(pageUrl);
                    HtmlDocument document = new HtmlDocument();
                    document.LoadHtml(html);
                    string pageXpath = "/html/body/section[1]/div/div[6]/a[@class='page-btn']";
                    HtmlNodeCollection nodes = document.DocumentNode.SelectNodes(pageXpath);
                    int pageCount = nodes.Select(a => Convert.ToInt32(a.InnerText)).OrderByDescending(a => a).FirstOrDefault();
                    #endregion 
                    for (int i = 1; i <= pageCount; i++)
                    {
                        Console.WriteLine($"开始抓取第{i}页的数据....");

                        string indexPageUrl = $"{pageUrl}&page={i}";
                        string pagehtml = HttpHelper.DownloadUrl(indexPageUrl);
                        HtmlDocument pageDocument = new HtmlDocument();
                        pageDocument.LoadHtml(pagehtml);
                        string liXpath = "/html/body/section[1]/div/div[4]/ul/li";         
                        HtmlNodeCollection pageliNodeList = pageDocument.DocumentNode.SelectNodes(liXpath);
                        foreach (var liNode in pageliNodeList)
                        {
                            CourseEntity course = new CourseEntity();
                            string imgXPath = "//*/a/img";
                            HtmlDocument imgDocument = new HtmlDocument();
                            imgDocument.LoadHtml(liNode.OuterHtml);
                            HtmlNode imgLoad = imgDocument.DocumentNode.SelectSingleNode(imgXPath);
                            if (imgLoad.Attributes["src"] != null)
                            {
                                var imgUrl = imgLoad.Attributes["src"].Value;
                                Console.WriteLine(imgUrl);
                                course.ImageUrl = imgUrl;
                                var title = imgLoad.Attributes["title"].Value;
                                Console.WriteLine(title);
                                course.Title = title;
                            }
                            HtmlDocument aDocument = new HtmlDocument();
                            aDocument.LoadHtml(liNode.OuterHtml);
                            string aXPath = "//*/a";
                            HtmlNode aLoad = aDocument.DocumentNode.SelectSingleNode(aXPath);
                            if (aLoad.Attributes["href"] != null)
                            {
                                string courseUrl = aLoad.Attributes["href"].Value;
                                Console.WriteLine(courseUrl);
                                course.Url = courseUrl;
                            }
                            if (aLoad.Attributes["data-id"] != null)
                            {
                                string courseId = aLoad.Attributes["data-id"].Value;
                                Console.WriteLine(courseId);

                                if (long.TryParse(courseId, out long longCouseId))
                                {
                                    course.CourseId = longCouseId;
                                }
                            }
                            courselist.Add(course);
                        }

                        Constant.CourseList = courselist;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("CrawlerMuti出现异常", ex);
                warnRepository.SaveWarn(category, string.Format("出现异常,Name={0} Level={1} Url={2}", category.Name, category.CategoryLevel, category.Url));
            }
        }


        /// <summary>
        /// 
        /// </summary>
        private void GetOnlyLiData(HtmlDocument document)
        {
            #region 获取一个课程的数据  
            {
                CourseEntity course = new CourseEntity();
                string lixPath = "/html/body/section[1]/div/div[5]/ul/li[1]";
                HtmlNode liLoad = document.DocumentNode.SelectSingleNode(lixPath);
                string imgXPath = "//*/a/img";
                HtmlDocument imgDocument = new HtmlDocument();
                imgDocument.LoadHtml(liLoad.OuterHtml);
                HtmlNode imgLoad = imgDocument.DocumentNode.SelectSingleNode(imgXPath);
                if (imgLoad.Attributes["src"] != null)
                {
                    var imgUrl = imgLoad.Attributes["src"].Value;
                    Console.WriteLine(imgUrl);
                    course.ImageUrl = imgUrl;
                    var title = imgLoad.Attributes["title"].Value;
                    Console.WriteLine(title);
                    course.Title = title;
                }
                HtmlDocument aDocument = new HtmlDocument();
                aDocument.LoadHtml(liLoad.OuterHtml);
                string aXPath = "//*/a";
                HtmlNode aLoad = aDocument.DocumentNode.SelectSingleNode(aXPath);
                if (aLoad.Attributes["href"] != null)
                {
                    string courseUrl = aLoad.Attributes["href"].Value;
                    Console.WriteLine(courseUrl);
                    course.Url = courseUrl;
                }
                if (aLoad.Attributes["data-id"] != null)
                {
                    string courseId = aLoad.Attributes["data-id"].Value;
                    Console.WriteLine(courseId);

                    if (long.TryParse(courseId, out long longCouseId))
                    {
                        course.CourseId = longCouseId;
                    }
                }
            }
            #endregion

        }


        ///// <summary>
        ///// 
        ///// </summary>
        //private void GetOnlyLiData()
        //{
        //    #region 获取一个课程的数据 
        //    //下载页面 
        //    string html = HttpHelper.DownloadUrl(category.Url);
        //    {
        //        //解析网页
        //        //1. Nuget引入HtmlAgilityPack
        //        HtmlDocument document = new HtmlDocument();
        //        document.LoadHtml(html);
        //        CourseEntity course = new CourseEntity();
        //        //2.通过Xpath解析
        //        string lixPath = "/html/body/section[1]/div/div[5]/ul/li[1]";
        //        HtmlNode liLoad = document.DocumentNode.SelectSingleNode(lixPath);
        //        string imgXPath = "//*/a/img";
        //        HtmlDocument imgDocument = new HtmlDocument();
        //        imgDocument.LoadHtml(liLoad.OuterHtml);
        //        HtmlNode imgLoad = imgDocument.DocumentNode.SelectSingleNode(imgXPath);
        //        if (imgLoad.Attributes["src"] != null)
        //        {
        //            var imgUrl = imgLoad.Attributes["src"].Value;
        //            Console.WriteLine(imgUrl);
        //            course.ImageUrl = imgUrl;
        //            var title = imgLoad.Attributes["title"].Value;
        //            Console.WriteLine(title);
        //            course.Title = title;
        //        }
        //        HtmlDocument aDocument = new HtmlDocument();
        //        aDocument.LoadHtml(liLoad.OuterHtml);
        //        string aXPath = "//*/a";
        //        HtmlNode aLoad = aDocument.DocumentNode.SelectSingleNode(aXPath);
        //        if (aLoad.Attributes["href"] != null)
        //        {
        //            string courseUrl = aLoad.Attributes["href"].Value;
        //            Console.WriteLine(courseUrl);
        //            course.Url = courseUrl;
        //        }
        //        if (aLoad.Attributes["data-id"] != null)
        //        {
        //            string courseId = aLoad.Attributes["data-id"].Value;
        //            Console.WriteLine(courseId);

        //            if (long.TryParse(courseId, out long longCouseId))
        //            {
        //                course.CourseId = longCouseId;
        //            }
        //        }
        //    }
        //    #endregion 
        //}


        #region 获取Ajax 请求数据
        /// <summary>
        /// 
        /// </summary>
        public void GetAjaxRequest()
        {
            string url = "https://ke.qq.com/cgi-bin/get_cat_info?bkn=315865921&r=0.6707674863900228";
            var ajaxData = HttpHelper.DownloadHtml(url, Encoding.UTF8);
            Hashtable hashtable = JsonConvert.DeserializeObject<Hashtable>(ajaxData);
            dynamic dynamicData = JsonConvert.DeserializeObject<dynamic>(ajaxData);
            string strResult = hashtable["result"].ToString();

            Hashtable hashResult = JsonConvert.DeserializeObject<Hashtable>(strResult);
            string strCatInfo = hashResult["catInfo"].ToString();
            Hashtable hashcatInfo = JsonConvert.DeserializeObject<Hashtable>(strCatInfo);

            List<TencentCategoryEntity> TencentCategoryList = new List<TencentCategoryEntity>();
            foreach (DictionaryEntry hashInfo in hashcatInfo)
            {
                #region 第一级类目 
                TencentCategoryEntity firstCategory = new TencentCategoryEntity();
                Hashtable hashValue = JsonConvert.DeserializeObject<Hashtable>(hashInfo.Value.ToString());
                //Console.WriteLine(hashValue["n"]);
                //Console.WriteLine(hashValue["o"]);  
                firstCategory.Name = hashValue["n"].ToString();
                firstCategory.Code = hashInfo.Key.ToString();
                firstCategory.CategoryLevel = 1;
                firstCategory.State = 1;

                TencentCategoryList.Add(firstCategory);
                #endregion
                #region 第二级类目 
                string secondCategoryValue = hashValue["s"].ToString();
                Hashtable hashSecondCategory = JsonConvert.DeserializeObject<Hashtable>(secondCategoryValue);
                foreach (DictionaryEntry secondcategoryItem in hashSecondCategory)
                {
                    TencentCategoryEntity secondCategory = new TencentCategoryEntity();
                    Hashtable hashSecond = JsonConvert.DeserializeObject<Hashtable>(secondcategoryItem.Value.ToString());
                    secondCategory.Code = secondcategoryItem.Key.ToString();
                    secondCategory.Name = hashSecond["n"].ToString();
                    secondCategory.CategoryLevel = 2;
                    secondCategory.State = 1;
                    secondCategory.ParentCode = hashInfo.Key.ToString();
                    TencentCategoryList.Add(secondCategory);
                }
                #endregion
            }
        }
        #endregion 
    }
}
